﻿using Prism.Events;
using Prism.Ioc;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using Xcj.Mes.Core.CustomControls;
using Xcj.Mes.WholeLine.ZH.HG.Shell.Events;

namespace Xcj.Mes.WholeLine.ZH.HG.Shell.Views.Main
{
    /// <summary>
    /// ToolBarView.xaml 的交互逻辑
    /// </summary>
    public partial class ToolBarView : UserControl
    {
        protected IEventAggregator _eventAggregator { get; private set; }

        public ToolBarView()
        {
            InitializeComponent();

            this.Loaded += ((o, e) =>
            {
                // 订阅事件
                _eventAggregator = ((Prism.PrismApplicationBase)Application.Current).Container.Resolve<IEventAggregator>();
            });
        }

        public async void OnClick(object sender, RoutedEventArgs e)
        {
            RadioButtonEx buttonEx = (RadioButtonEx)sender;
            await Task.Run(() =>
            {
                ThreadPool.QueueUserWorkItem(delegate
                {
                    try
                    {
                        SynchronizationContext.SetSynchronizationContext(new DispatcherSynchronizationContext(Application.Current.Dispatcher));
                        SynchronizationContext.Current?.Post(p =>
                        {
                            try { _eventAggregator?.GetEvent<ToolBarSelectedEvent>()?.Publish(buttonEx.Text); } catch { }
                        }, null);
                    }
                    catch { }
                });
            });
        }
    }
}
